#!/bin/bash

#
# convert2bed
# Copyright (C) 2014-2016 Alex Reynolds
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

cmd="convert2bed"
input_format="bam"
output_format="bed"

# general

do_not_sort=false
max_mem_set=false
max_mem="2G"
sort_tmpdir_set=false
sort_tmpdir="/tmp"

# format-specific

all_reads=false
keep_header=false
split=false
split_with_deletions=false
reduced=false

help()
{
    ${cmd} --help-bam
    exit $1
}

optspec=":r:m:dhaksSR-:"
while getopts "$optspec" optchar; do
    case "${optchar}" in
        -)
            case "${OPTARG}" in
                sort-tmpdir)
                    val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
                    sort_tmpdir=${val}
                    sort_tmpdir_set=true
                    ;;
                sort-tmpdir=*)
                    val=${OPTARG#*=}
                    sort_tmpdir=${val}
                    sort_tmpdir_set=true
                    ;;
                max-mem)
                    val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
                    max_mem=${val}
                    max_mem_set=true
                    ;;
                max-mem=*)
                    val=${OPTARG#*=}
                    max_mem=${val}
                    max_mem_set=true
                    ;;
                do-not-sort)
                    do_not_sort=true
                    ;;
                help)
                    val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
                    #echo "Parsing option: '--${OPTARG}', value: '${val}'" >&2;
                    help 0
                    ;;
                help=*)
                    val=${OPTARG#*=}
                    opt=${OPTARG%=$val}
                    #echo "Parsing option: '--${opt}', value: '${val}'" >&2
                    help 0
                    ;;
                help-bam)
                    help 0
                    ;;
                all-reads)
                    all_reads=true
                    ;;
                keep-header)
                    keep_header=true
                    ;;
                split)
                    split=true
                    ;;
                split-with-deletions)
                    split_with_deletions=true
                    ;;
                reduced)
                    reduced=true
                    ;;
                *)
                    help 1
                    #if [ "$OPTERR" = 1 ] && [ "${optspec:0:1}" != ":" ]; then echo "Unknown option --${OPTARG}" >&2; fi
                    ;;
            esac;;
        r)
            sort_tmpdir=${OPTARG}
            sort_tmpdir_set=true
            ;;
        m)
            max_mem=${OPTARG}
            max_mem_set=true
            ;;
        d)
            do_not_sort=true
            ;;
        h)
            help 0
            ;;
        a)
            all_reads=true
            ;;
        k)
            keep_header=true
            ;;
        s)
            split=true
            ;;
        S)
            split_with_deletions=true
            ;;
        R)
            reduced=true
            ;;
        *)
            help 1
            #if [ "$OPTERR" != 1 ] || [ "${optspec:0:1}" = ":" ]; then echo "Non-option argument: '-${OPTARG}'" >&2; fi
            ;;
    esac
done

# base options

options="--input=${input_format} --output=${output_format}"

# general

if [ "${max_mem_set}" = true ]; then options="${options} --max-mem=${max_mem}"; fi
if [ "${sort_tmpdir_set}" = true ]; then options="${options} --sort-tmpdir=${sort_tmpdir}"; fi
if [ "${do_not_sort}" = true ]; then options="${options} --do-not-sort"; fi

# format-specific

if [ "${all_reads}" = true ]; then options="${options} --all-reads"; fi
if [ "${keep_header}" = true ]; then options="${options} --keep-header"; fi
if [ "${split}" = true ]; then options="${options} --split"; fi
if [ "${split_with_deletions}" = true ]; then options="${options} --split-with-deletions"; fi
if [ "${reduced}" = true ]; then options="${options} --reduced"; fi

${cmd} ${options} - <&0
